#include "ui.h"

extern RTC_HandleTypeDef hrtc;
struct tm CurrentTime;
char timeChars[9];

extern uint16_t defaultPWM;
static lv_obj_t* slider_label;
lv_obj_t* timeLable;

static void slider_event_cb(lv_event_t* e);
static void set_value(void* bar, int32_t v);
static void update_time_cb(lv_timer_t *timer);
static void check_time_cb(lv_timer_t *timer);
static void time_set_btn_cb(lv_event_t* e);
static void time_set_page(void);
static void roller_event_handler(lv_event_t* e);

uint32_t time_cnt = 1672588795;

void lv_example(void)
{
    
//    LV_IMG_DECLARE(img_th);
//    lv_obj_t *img = lv_img_create(lv_scr_act());
//    lv_img_set_src(img, &img_th);
//    lv_obj_center(img);
	
	lv_obj_t* slider = lv_slider_create(lv_scr_act());
    lv_obj_set_size(slider, 780, 10);
	// lv_obj_set_style_opa(slider, 100, NULL);
    lv_obj_align(slider, LV_ALIGN_TOP_MID, 0, 10);
    lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_slider_set_value(slider, defaultPWM, NULL);

    /* Create a label below the slider */
    slider_label = lv_label_create(lv_scr_act());
    char buf[8];
    lv_snprintf(buf, sizeof(buf), "%d%%", (int)lv_slider_get_value(slider));
    lv_label_set_text(slider_label, buf);

    lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    timeLable = lv_label_create(lv_scr_act());
	lv_obj_set_style_text_font(timeLable, &lv_font_montserrat_48, 0);
    lv_obj_set_width(timeLable, 220);
    time_cnt = rtc_GetCounter();
    CurrentTime = *localtime(&time_cnt);
    strftime(timeChars, 8, "%H:%M:%S", &CurrentTime);
    lv_label_set_text(timeLable, timeChars);
    lv_obj_align_to(timeLable, lv_scr_act(), LV_ALIGN_BOTTOM_LEFT, 10, 0);

    // lv_obj_t* btn_TimeSet = lv_btn_create(lv_scr_act());
    // lv_obj_align(btn_TimeSet, LV_ALIGN_LEFT_MID, 10, 0);
    // lv_obj_t* btn_label = lv_label_create(btn_label);
    // lv_label_set_text(btn_label, "Time");
    // lv_obj_center(btn_label);
    // lv_obj_add_event_cb(btn_TimeSet, time_set_btn_cb, LV_EVENT_CLICKED, NULL);

    lv_timer_t* timer1 = lv_timer_create(update_time_cb, 1000, NULL);
	lv_timer_t* timer2 = lv_timer_create(check_time_cb, 10000, NULL);
}

static void update_time_cb(lv_timer_t *timer)
{
    // HAL_RTC_GetTime(&hrtc, &Time_Struct, RTC_FORMAT_BCD);
    // lv_snprintf(timeChars, sizeof(timeChars), "%2d:%2d:%2d",Time_Struct.Hours, Time_Struct.Minutes, Time_Struct.Seconds);
    // lv_label_set_text_fmt(timeLable, "%d:%d:%d", Time_Struct.Hours, Time_Struct.Minutes, Time_Struct.Seconds);
    time_cnt++;
    CurrentTime = *localtime(&time_cnt);
    strftime(timeChars, 8, "%H:%M:%S", &CurrentTime);
    lv_label_set_text(timeLable, timeChars);
}

static void check_time_cb(lv_timer_t *timer)
{
    uint32_t i = rtc_GetCounter();
    if (i != time_cnt) {
        time_cnt = i;
    }
}

static void slider_event_cb(lv_event_t* e)
{
    lv_obj_t* slider = lv_event_get_target(e);
    char buf[8];
    lv_snprintf(buf, sizeof(buf), "%d%%", (int)lv_slider_get_value(slider));
    lv_label_set_text(slider_label, buf);
    lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    uint16_t sliderValue = (int)lv_slider_get_value(slider);
    uint16_t pwmValue = lv_map(sliderValue, 0, 100, 0, 100);
    LCD_PWMinit(pwmValue);
}

static void time_set_btn_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED) {
        time_set_page();
    }
}

static void time_set_page(void)
{
    /* A style to make the selected option larger */
    static lv_style_t style_sel;
    lv_style_init(&style_sel);
    lv_style_set_text_font(&style_sel, &lv_font_montserrat_22);

    const char * opts = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23";
    lv_obj_t* roller;
    /* A roller on the left with aligned text, and custom width */
    roller = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller, opts, LV_ROLLER_MODE_NORMAL);
    lv_roller_set_visible_row_count(roller, 5);
    lv_obj_set_width(roller, 10);
    lv_obj_add_style(roller, &style_sel, LV_PART_SELECTED);
    lv_obj_set_style_align(roller, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(roller, LV_ALIGN_LEFT_MID, 10, 0);
    lv_obj_add_event_cb(roller, roller_event_handler, LV_EVENT_ALL, NULL);
    lv_roller_set_selected(roller, 2, LV_ANIM_OFF);
}

static void roller_event_handler(lv_event_t* e)
{}
